<?php

use think\migration\Migrator;

class CreateCheckinTables extends Migrator
{
    /**
     * 执行迁移
     */
    public function change()
    {
        // 1. 门店表
        $this->createStoreTable();
        
        // 2. 会员表
        $this->createMemberTable();
        
        // 3. 会员门店关系表
        $this->createMemberStoreTable();
        
        // 4. 打卡记录表
        $this->createCheckinRecordTable();
        
        // 5. 积分明细表
        $this->createPointsRecordTable();
        
        // 6. 礼品表
        $this->createGiftTable();
        
        // 7. 兑换记录表
        $this->createExchangeRecordTable();
        
        // 8. 规则设置表
        $this->createSettingTable();
    }
    
    /**
     * 创建门店表
     */
    private function createStoreTable()
    {
        $table = $this->table('checkin_store', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '门店表',
        ]);
        
        $table->addColumn('name', 'string', ['limit' => 100, 'comment' => '门店名称'])
              ->addColumn('code', 'string', ['limit' => 50, 'null' => true, 'comment' => '门店编码'])
              ->addColumn('contact', 'string', ['limit' => 50, 'null' => true, 'comment' => '联系人'])
              ->addColumn('phone', 'string', ['limit' => 20, 'null' => true, 'comment' => '联系电话'])
              ->addColumn('address', 'string', ['limit' => 200, 'null' => true, 'comment' => '门店地址'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态(1正常,0禁用)'])
              ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
              ->addColumn('remark', 'string', ['limit' => 500, 'null' => true, 'comment' => '备注'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
              ->addIndex(['code'], ['unique' => true])
              ->addIndex(['status'])
              ->create();
    }
    
    /**
     * 创建会员表
     */
    private function createMemberTable()
    {
        $table = $this->table('checkin_member', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '会员表',
        ]);
        
        $table->addColumn('register_store_id', 'integer', ['default' => 0, 'comment' => '注册门店ID'])
              ->addColumn('name', 'string', ['limit' => 50, 'comment' => '会员姓名'])
              ->addColumn('phone', 'string', ['limit' => 20, 'null' => true, 'comment' => '手机号'])
              ->addColumn('card_no', 'string', ['limit' => 50, 'comment' => '磁卡号'])
              ->addColumn('face_data', 'text', ['null' => true, 'comment' => '人脸数据(预留)'])
              ->addColumn('total_points', 'integer', ['default' => 0, 'comment' => '累计总积分'])
              ->addColumn('used_points', 'integer', ['default' => 0, 'comment' => '已使用积分'])
              ->addColumn('available_points', 'integer', ['default' => 0, 'comment' => '可用积分'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态(1正常,0禁用)'])
              ->addColumn('remark', 'string', ['limit' => 500, 'null' => true, 'comment' => '备注'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '注册时间'])
              ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
              ->addIndex(['card_no'], ['unique' => true])
              ->addIndex(['phone'])
              ->addIndex(['register_store_id'])
              ->addIndex(['status'])
              ->create();
    }
    
    /**
     * 创建会员门店关系表
     */
    private function createMemberStoreTable()
    {
        $table = $this->table('checkin_member_store', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '会员门店关系表',
        ]);
        
        $table->addColumn('member_id', 'integer', ['comment' => '会员ID'])
              ->addColumn('store_id', 'integer', ['comment' => '门店ID'])
              ->addColumn('first_time', 'datetime', ['null' => true, 'comment' => '首次打卡时间'])
              ->addColumn('last_time', 'datetime', ['null' => true, 'comment' => '最后打卡时间'])
              ->addColumn('checkin_count', 'integer', ['default' => 0, 'comment' => '打卡次数'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addIndex(['member_id', 'store_id'], ['unique' => true])
              ->addIndex(['store_id'])
              ->create();
    }
    
    /**
     * 创建打卡记录表
     */
    private function createCheckinRecordTable()
    {
        $table = $this->table('checkin_record', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '打卡记录表',
        ]);
        
        $table->addColumn('member_id', 'integer', ['comment' => '会员ID'])
              ->addColumn('store_id', 'integer', ['comment' => '打卡门店ID'])
              ->addColumn('card_no', 'string', ['limit' => 50, 'comment' => '磁卡号'])
              ->addColumn('start_time', 'datetime', ['null' => true, 'comment' => '开始时间'])
              ->addColumn('end_time', 'datetime', ['null' => true, 'comment' => '结束时间'])
              ->addColumn('duration_minutes', 'integer', ['default' => 0, 'comment' => '时长(分钟)'])
              ->addColumn('points', 'integer', ['default' => 0, 'comment' => '获得积分'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 3, 'comment' => '状态(1正常,2无效,3进行中)'])
              ->addColumn('invalid_reason', 'string', ['limit' => 200, 'null' => true, 'comment' => '无效原因'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addIndex(['member_id'])
              ->addIndex(['store_id'])
              ->addIndex(['card_no'])
              ->addIndex(['status'])
              ->addIndex(['start_time'])
              ->create();
    }
    
    /**
     * 创建积分明细表
     */
    private function createPointsRecordTable()
    {
        $table = $this->table('checkin_points_record', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '积分明细表',
        ]);
        
        $table->addColumn('member_id', 'integer', ['comment' => '会员ID'])
              ->addColumn('store_id', 'integer', ['comment' => '关联门店ID'])
              ->addColumn('points', 'integer', ['comment' => '积分数(正数增加,负数扣除)'])
              ->addColumn('type', 'integer', ['limit' => 1, 'comment' => '类型(1获得,2消费,3调整)'])
              ->addColumn('source', 'string', ['limit' => 20, 'comment' => '来源(checkin打卡,exchange兑换,adjust调整)'])
              ->addColumn('related_id', 'integer', ['default' => 0, 'comment' => '关联ID(打卡记录ID/兑换记录ID)'])
              ->addColumn('expire_time', 'datetime', ['null' => true, 'comment' => '过期时间(NULL为永久)'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态(1有效,0已使用/过期)'])
              ->addColumn('remark', 'string', ['limit' => 200, 'null' => true, 'comment' => '备注'])
              ->addColumn('operator_id', 'integer', ['default' => 0, 'comment' => '操作人ID'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addIndex(['member_id'])
              ->addIndex(['store_id'])
              ->addIndex(['type'])
              ->addIndex(['source'])
              ->addIndex(['status'])
              ->addIndex(['expire_time'])
              ->create();
    }
    
    /**
     * 创建礼品表
     */
    private function createGiftTable()
    {
        $table = $this->table('checkin_gift', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '礼品表',
        ]);
        
        $table->addColumn('name', 'string', ['limit' => 100, 'comment' => '礼品名称'])
              ->addColumn('type', 'string', ['limit' => 50, 'null' => true, 'comment' => '礼品类型'])
              ->addColumn('points_required', 'integer', ['comment' => '所需积分'])
              ->addColumn('stock', 'integer', ['default' => -1, 'comment' => '库存(-1为不限)'])
              ->addColumn('image', 'string', ['limit' => 500, 'null' => true, 'comment' => '图片'])
              ->addColumn('description', 'text', ['null' => true, 'comment' => '描述'])
              ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
              ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态(1上架,0下架)'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
              ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
              ->addIndex(['status'])
              ->addIndex(['sort'])
              ->create();
    }
    
    /**
     * 创建兑换记录表
     */
    private function createExchangeRecordTable()
    {
        $table = $this->table('checkin_exchange_record', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '兑换记录表',
        ]);
        
        $table->addColumn('member_id', 'integer', ['comment' => '会员ID'])
              ->addColumn('store_id', 'integer', ['comment' => '兑换门店ID'])
              ->addColumn('gift_id', 'integer', ['comment' => '礼品ID'])
              ->addColumn('gift_name', 'string', ['limit' => 100, 'comment' => '礼品名称(冗余)'])
              ->addColumn('points', 'integer', ['comment' => '消耗积分'])
              ->addColumn('operator_id', 'integer', ['comment' => '操作员ID'])
              ->addColumn('remark', 'string', ['limit' => 500, 'null' => true, 'comment' => '备注'])
              ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '兑换时间'])
              ->addIndex(['member_id'])
              ->addIndex(['store_id'])
              ->addIndex(['gift_id'])
              ->addIndex(['create_time'])
              ->create();
    }
    
    /**
     * 创建规则设置表
     */
    private function createSettingTable()
    {
        $table = $this->table('checkin_setting', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '规则设置表',
        ]);
        
        $table->addColumn('store_id', 'integer', ['default' => 0, 'comment' => '门店ID(0为全局设置)'])
              ->addColumn('key', 'string', ['limit' => 50, 'comment' => '配置键'])
              ->addColumn('value', 'text', ['null' => true, 'comment' => '配置值'])
              ->addColumn('description', 'string', ['limit' => 200, 'null' => true, 'comment' => '说明'])
              ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
              ->addIndex(['store_id', 'key'], ['unique' => true])
              ->create();
    }
}

